<?php
/**
 * 后台专辑管理控制器。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace App\Http\Controllers\Shadow;

use View;
use Config;
use Response;
use App\Http\Model\Tag;
use App\Http\Model\Type;
use App\Http\Model\Album;
use App\Http\Model\AlbumTag;
use Illuminate\Http\Request;

class AlbumController extends ShadowController
{
    public function onGet()
    {
        $this->results['tags'] = Tag::where('status', '=', 'enable')->get();
        $this->results['types'] = Type::where('status', '=', 'enable')->get();
        $this->results['token'] = csrf_token();
        $this->results['albums'] = Album::all();
        
        return View::make('shadow/album', $this->results);
    }
    
    public function onPost(Request $request)
    {
        $s_type = $request->get('type', '');
        $a_tags = $request->get('tag', array());
        if ($s_type == 'add_album') {
            $o_album = new Album();
            $o_album->title = $request->get('title', '');
            $o_album->cover = $request->get('cover', '');
            $o_album->description = $request->get('description', '');
            $o_album->type_id = $request->get('phototype', '');
            $o_album->uid = $this->user->uid;
            $o_album->save();
        }
        
        //查询是否关联标签
        $o_album_tags = AlbumTag::where('pa_id', '=', $o_album->pa_id)->get();
        if (count($o_album_tags)) {
        }
        
        //关联标签
        foreach ($a_tags as $vv) {
            $o_album_tag = new AlbumTag();
            $o_album_tag->pa_id = $o_album->pa_id;
            $o_album_tag->vt_id = $vv;
            $o_album_tag->save();
        }
        
        return Redirect::to('/shadow/photo/album');
    }
}
